package cn.aws360.boot.system.biz.service.impl;

import cn.aws360.boot.base.api.exception.ServiceException;
import cn.aws360.boot.data.api.factory.PageFactory;
import cn.aws360.boot.data.api.pojo.request.PageQuery;
import cn.aws360.boot.data.api.pojo.response.PageResult;
import cn.aws360.boot.data.api.utils.PojoUtils;
import cn.aws360.boot.system.api.exception.SysExceptionEnum;
import cn.aws360.boot.system.api.pojo.request.SysLogsQuery;
import cn.aws360.boot.system.api.pojo.response.SysLogsModel;
import cn.aws360.boot.system.biz.domain.SysLogs;
import cn.aws360.boot.system.biz.mapper.SysLogsMapper;
import cn.aws360.boot.system.biz.service.SysLogsService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author junnan
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysLogsServiceImpl extends ServiceImpl<SysLogsMapper, SysLogs> implements SysLogsService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(String ids) {
        List<Integer> logsIdList = Convert.toList(Integer.class, ids.split(StrUtil.COMMA));
        return this.getBaseMapper().deleteBatchIds(logsIdList) > 0;
    }

    @Override
    public SysLogsModel detail(Integer id) {
        SysLogs sysLogs = this.getSysLogsById(id);
        return BeanUtil.copyProperties(sysLogs, SysLogsModel.class);
    }

    @Override
    public PageResult<SysLogsModel> findPage(SysLogsQuery sysLogsQuery, PageQuery pageQuery) {
        LambdaQueryWrapper<SysLogs> queryWrapper = createQueryWrapper(sysLogsQuery);
        PageResult<SysLogs> sysLogsPage = PageFactory.getPageResult(this, pageQuery, queryWrapper);
        return PojoUtils.copyToPage(sysLogsPage, SysLogsModel.class);
    }

    private SysLogs getSysLogsById(Integer id) {
        SysLogs sysLogs = this.getById(id);
        checkNotExist(sysLogs);
        return sysLogs;
    }

    private LambdaQueryWrapper<SysLogs> createQueryWrapper(SysLogsQuery logsQuery) {
        LambdaQueryWrapper<SysLogs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysLogs::getEpoch);
        String epoch = logsQuery.getEpoch();
        if (StrUtil.isNotBlank(epoch) && epoch.contains(StrUtil.COMMA)) {
            String[] epochArr = epoch.split(StrUtil.COMMA);
            LocalDateTime begTime = DateUtil.date(Long.parseLong(epochArr[0]) * 1000).toTimestamp().toLocalDateTime();
            LocalDateTime endTime = DateUtil.date(Long.parseLong(epochArr[1]) * 1000).toTimestamp().toLocalDateTime();
            queryWrapper.ge(SysLogs::getEpoch, begTime).le(SysLogs::getEpoch, endTime);
        }
        queryWrapper.like(ObjectUtil.isNotEmpty(logsQuery.getTitle()), SysLogs::getTitle, logsQuery.getTitle());
        queryWrapper.eq(ObjectUtil.isNotNull(logsQuery.getOpsType()), SysLogs::getOpsType, logsQuery.getOpsType());
        queryWrapper.eq(ObjectUtil.isNotNull(logsQuery.getResType()), SysLogs::getResType, logsQuery.getResType());
        return queryWrapper;
    }

    private void checkNotExist(SysLogs sysLogs) {
        if (ObjectUtil.isNull(sysLogs)) {
            throw new ServiceException(SysExceptionEnum.LOGS_NOT_EXIST);
        }
    }

}
